import { defineStore } from "pinia";
import vueAxios from "@/api/index";
import ACrypto from "@/utils/crypto";
import { DICTIONARY_OBJ } from "../mutation-types";
import { DictionaryState } from "@/stores/interface";
export const useDictionryStore = defineStore({
  id: "dictionry",
  state: (): DictionaryState => ({
    dictionaryObj: {}, // 字典数据
    ruleList: [], // 字段规则
    scanFileNodes: [], // 扫描文件节点
    strFileNameWithoutExt: "", // 文件名
    fileDir: "", // 文件目录名
    strExtensionValue: "pdf" // 文件后缀名
  }),
  getters: {
    getDictionaryObj(state) {
      if (Object.keys(state.dictionaryObj).length === 0) {
        const newDictionaryObj = ACrypto.getSS(window.sessionStorage.getItem(DICTIONARY_OBJ) as string, DICTIONARY_OBJ);
        state.dictionaryObj = newDictionaryObj || {};
      }
      return state.dictionaryObj;
    }
  },
  actions: {
    async setDictionaryObj() {
      try {
        const Res = await vueAxios.get("Master_Base_DataDictionary/Get", { order: "DD_Order asc" }, { noLoading: true });
        const Res2 = await vueAxios.get("Master_Base_DDL/Get", { order: "DDL_Order asc" }, { noLoading: true });
        const resultObj = {} as any;
        if (Res.List) {
          for (let i = 0; i < Res.List.length; i++) {
            resultObj[Res.List[i].DD_Code] = await Res2.List.filter((item2: any) => item2.DD_ID === Res.List[i].DD_ID);
          }
          this.dictionaryObj = resultObj;
          ACrypto.setSS(resultObj, DICTIONARY_OBJ);
        }
      } catch (error) {
        // 让表单组件显示错误
        return error;
      }
    },
    // 获取字段规则
    getRuleList(data: any[]) {
      this.ruleList = data;
    },
    // 设置扫描节点
    setFileNodes(nodes: any[]) {
      this.scanFileNodes = nodes;
    },
    // 设置文件目录名称
    setFileDirName(name: string) {
      this.fileDir = name;
    },
    // 设置文件名称
    setFileName(name: string) {
      this.strFileNameWithoutExt = name;
    },
    // 设置文件扩展名
    setExtensionValue(value: string) {
      this.strExtensionValue = value;
    }
  }
});
